//
//  AgentPermanceDetailVC.swift
//  kxf
//
//  Created by Mac on 2024/3/26.
//

import Foundation
import MCToast

class AgentPermanceDetailVC : WHBaseViewVC {
    
    var startTime = Date().getLast30Days()
    var endTime = Date().todayDate
    var phone = ""
    var name = ""
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initUI()
        sendRequest()
    }
    lazy var topVm : PerformanceFilterVM = {
        let vm = PerformanceFilterVM.init(frame: CGRect.init(x: 0, y: 0, width:0, height: 0))
        vm.dataTitleVm.titleVm.titleLabel.text = "时间"
        vm.timeFilterAlertVm.controller = self
        vm.controller = self
        vm.timeFilterAlertVm.queryBlock = {(startTime,endTime)in
            self.topVm.dataTitleVm.contentLabel.text = "\(startTime)  -  \(endTime)"
            self.startTime = startTime
            self.endTime = endTime
            self.sendRequest()
        }
        
        vm.queryBlock = {(startTime,endTime)in
            self.topVm.dataTitleVm.contentLabel.text = "\(startTime)  -  \(endTime)"
            self.startTime = startTime
            self.endTime = endTime
            self.sendRequest()
        }
        return vm
    }()
    lazy var rebateVm : PermanceAgentTopVM = {
        let vm = PermanceAgentTopVM.init(frame: CGRect.init(x: 0, y: self.topVm.frame.maxY, width: 0, height: 0))
        vm.nameLabel.text = "\(name)"
        return vm
    }()
    lazy var mercMsgVm : PermanceMercMsgVM = {
        let vm = PermanceMercMsgVM.init(frame: CGRect.init(x: 0, y: self.rebateVm.frame.maxY + kFitWidth(10), width: 0, height: 0))
        
        return vm
    }()
}

extension AgentPermanceDetailVC{
    func initUI(){
        initNavi(titleStr: "\(name)团队业绩详情")
        view.addSubview(scrollView)
        scrollView.frame = CGRect.init(x: 0, y: getNavigationBarHeight(), width: SCREEN_WIDHT, height: SCREEN_HEIGHT-getNavigationBarHeight())
        scrollView.addSubview(topVm)
        scrollView.addSubview(rebateVm)
        scrollView.addSubview(mercMsgVm)
    }
}

extension AgentPermanceDetailVC{
    func sendRequest(){
        MCToast.mc_loading()
        let param = ["startTime":"\(startTime)",
                     "endTime":"\(endTime)",
                     "agentPhone":"\(phone)",
                     "agentName":"\(name)"]
        let urlString = "\(URL_subAgentStatHistoryDetail)?agentPhone=\(phone)&agentName=\(name.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? "")&startTime=\(startTime)&endTime=\(endTime)"
        WHNetworkUtil.shareManager().GET(urlString: urlString) { responseObject in
            DLLog(message: "\(responseObject)")
            
            if responseObject["code"]as? Int ?? 0 == 200 {
                let dataObj = responseObject["data"]as? NSDictionary ?? [:]
                let subAgentDevProfitResult = dataObj["subAgentDevProfitResult"]as? NSDictionary ?? [:]
                self.rebateVm.updateUI(dict: subAgentDevProfitResult)
                self.mercMsgVm.updateUI(dict: subAgentDevProfitResult)
            }else{
                self.presentAlertVcNoAction(title: "\(responseObject["message"]as? String ?? "网络异常，请稍后重试")", viewController: self)
            }
        }
    }
}
